const domain = 'https://share.biyong007.com' // 请求域名http://47.119.152.250
const tokenKey = 'Authorization' // token的key
const token = ''//'BearereyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImNyZWF0ZWQiOjE2Mzk5MDYyNTc3ODgsImV4cCI6MTY0MDUxMTA1N30.WcExrimLhIhn1K26QUAuZz9Fa522JrlPUGl8hwBEhtzYxb9is0JHAVfSCchxGqpsVzI9lHtM-mbf4T49mZ2pYA' // token
const sign = '' // 签名
const header = {
	'content-type': 'application/json; charset=utf-8'
}
header[tokenKey] = token
const exceptionAddrArr = ['/user/login']

function checkToken(url) {
	if (!exceptionAddrArr.includes(url)) {
		// 校验token
	}
}

function request({ method, url, data, formReq = false}) {
	checkToken(url)
	if (formReq) {
		header['content-type'] = 'application/x-www-form-urlencoded'
	}
	return new Promise((resolve, reject) => {
		uni.showLoading({ title: '加载中'})
		uni.request({
			url: domain + url,
			method: method,
			data,
			header,
			success(res) {
				uni.hideLoading()
				const { data } = res
				resolve(data || res)
			},
			fail(err) {
				uni.hideLoading()
				reject(err)
			}
		})
	})
}

module.exports.http = {
	get({ url, data }) {
		return request({ method: 'GET', url, data})
	},
	post({ url, data }) {
		return request({ method: 'POST', url, data})
	},
	form({ url, data }) {
		return request({ method: 'POST', url, data, formReq: true })
	}
}